package com.solution.framework.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * Busline: sunguangzhu
 * Date: 13-7-2
 * Time: 上午10:59
 * To change this template use File | Settings | File Templates.
 */
public class RequestVarsUtil {
    public static com.solution.framework.util.Pagination getPagination(HttpServletRequest request) {

        int page = 0;
        int pageSize = 10;

        if (request.getParameter("page") != null && !request.getParameter("page").equals("")) {
            page = Integer.valueOf(request.getParameter("page"));
        }
        if (request.getParameter("rows") != null && !request.getParameter("rows").equals("")) {
            pageSize = Integer.valueOf(request.getParameter("rows"));
        }
            /* if(null!=request.getParameter("order")) {

                 order=request.getParameter("order").toString();

             }

             if(null!=request.getParameter("sort")) {

                 order=request.getParameter("sort").toString();

             }*/
        com.solution.framework.util.Pagination pagination = new com.solution.framework.util.Pagination(page, pageSize);
        return pagination;
    }

    public static Long getLong(HttpServletRequest request, String str) {
        String paramStr = request.getParameter(str);
        if (StringUtils.isBlank(paramStr)) {
            paramStr = "0";
        }
        return Long.valueOf(paramStr);
    }
    public static String getString(HttpServletRequest request, String str) {
        String paramStr = request.getParameter(str);
        if (StringUtils.isBlank(paramStr)) {
            paramStr = "";
        }
        return paramStr;
    }
    public static void setPagination(Pagination pagination, int total) {
        pagination.setAllPage((total % pagination.getPageSize() == 0 ? total
                / pagination.getPageSize() : total / pagination.getPageSize() + 1));
        if (pagination.getPage() >= pagination.getAllPage()) {
            pagination.setPage(pagination.getAllPage());
            pagination.setNextPage(pagination.getAllPage());
        } else {
            pagination.setNextPage(pagination.getPage() + 1);
        }
        if (pagination.getPage() <= 1) {
            pagination.setPage(1);
            pagination.setBackPage(1);
        } else {
            pagination.setBackPage(pagination.getPage() - 1);
        }
        pagination.setFinalPage(pagination.getAllPage());
    }

}
